<?php
# @version SVN: $Id: search.inc 583 2012-04-05 15:22:45Z novachok $

# QUICK SEARCH FOR ARTICLE
if(is_numeric($REQUEST->getRequestLast()) && $REQUEST->getRequestLast() != 0)
{
   $DB->sql = 'SELECT `cat` FROM `meccano_articles` WHERE `id` = '. $REQUEST->getRequestLast() .' LIMIT 0,1';
   $DB->DB_Query();
   $cat = $DB->DB_GetResult();
   if(!empty($cat) && isset($categories[$cat]))
   {
      header('location: '. $categories[$cat]->url . $REQUEST->getRequestLast() .'.html');
      exit();
   }
}


# REGULAR SEARCH
$gQuery['q'] = !empty($_GET['query']) ? strip_tags(mb_strtolower($_GET['query'], 'UTF-8')) : false;
$gQuery['c'] = isset($_GET['c']) && $FILTER->isValidInteger($_GET['c']) ? $_GET['c'] : FALSE;

$data = array();

if (!empty($gQuery['q']) && config::read('enabled', 'search'))
{
   # Lucene search
   $SEARCH = new search();

   if ($gQuery['c'] !== FALSE)
      $SEARCH->addCondition($gQuery['c'], 'category');

   $data = $SEARCH->search($gQuery['q']);
   
   
} elseif (!empty($gQuery['q']))
{

   // TODO: refactoring GOOGLE search methods using approach of Internal Search class and Paginator class

   # Make query to google
   $gQuery['start'] = !empty($_GET['p']) ? $FILTER->getFirstInteger($_GET['p']) : 0;
   $gQuery['site'] = 'http://' . $_SERVER['HTTP_HOST'];
   $gQuery['rsz'] = 'large';

   $url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0';
   if (!empty($gQuery['rsz']))
      $url .= '&rsz=' . $gQuery['rsz'];
   $url .= '&start=' . $gQuery['start'] . '&q=' . urlencode($gQuery['q'] . ' site:' . $gQuery['site']);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_REFERER, $gQuery['site']);
   $body = curl_exec($ch);
   curl_close($ch);
   $json = json_decode($body);


   # Make results array
   $data['results'] = array();
   if (count($json->responseData->results) > 0)
   {
      foreach ($json->responseData->results as $v)
      {
         $data['results'][] = array(
             'href' => $v->unescapedUrl,
             'url' => $v->url,
             'visibleUrl' => $v->visibleUrl,
             'cacheUrl' => $v->cacheUrl,
             'title' => $v->title,
             'description' => $v->content
         );
      }
   }

   # Make pages array
   if (count($json->responseData->results) > 0)
   {
      $data['pages'] = array();
      $url = $gQuery['site'] . $categories[9]->href . '?query=' . $gQuery['q'];
      foreach ($json->responseData->cursor->pages as $v)
      {
         $data['pages']['links'][] = array(
             'current' => FALSE,
             'href' => $url . '&p=' . $v->start,
             'num' => $v->label
         );
      }
   }

   $currentPageIndex = $json->responseData->cursor->currentPageIndex;
   $data['pages']['next'] = (count($data['pages']) > $currentPageIndex + 1) ? $data['pages'][$currentPageIndex + 1]['startUrl'] : FALSE;
   $data['pages']['prev'] = ($currentPageIndex) ? $data['pages'][$currentPageIndex - 1]['startUrl'] : FALSE;
   $data['pages']['links'][$currentPageIndex]['current'] = TRUE;
   $data['pages']['onpage'] = 8;
   $data['pages']['found'] = $json->responseData->cursor->estimatedResultCount;
}

$data['query'] = array(
    'query' => $gQuery['q'],
    'category' => $gQuery['c']
);


$SMARTY->assign('data', $data);